/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_framework/zhiqim_orm.htm
 *
 * Zhiqim Orm is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.orm.dbo;

import java.sql.CallableStatement;
import java.sql.SQLException;

import org.zhiqim.orm.ORMConstants;
import org.zhiqim.orm.datasource.ZCall;

/**
 * 调用参数
 *
 * @version v1.4.1 @author zouzhigang 2018-7-23 新建与整理
 */
public final class CallParam implements ORMConstants
{
    private CALL_TYPE type;
    private Class<?> clazz;
    private Object value;
    
    private CallParam(CALL_TYPE type, Class<?> clazz)
    {
        this.type = type;
        this.clazz = clazz;
    }
    
    private CallParam(CALL_TYPE type, Object value)
    {
        this.type = type;
        this.value = value;
        this.clazz = value.getClass();
    }
    
    public CALL_TYPE type()
    {
        return type;
    }
    
    public Object value()
    {
        return value;
    }
    
    /**********************************************************************************/
    //三种生成参数对象
    /**********************************************************************************/
    
    public boolean isInWrite()
    {
        return this.type == CALL_TYPE.IN || this.type == CALL_TYPE.INOUT;
    }
    
    public boolean isOutRead()
    {
        return this.type == CALL_TYPE.OUT || this.type == CALL_TYPE.INOUT;
    }
    
    public int typeSQL()
    {
        return ZCall.getType(clazz);
    }
    
    public void value(CallableStatement cstmt, int index) throws SQLException
    {
        value = ZCall.getValue(clazz, cstmt, index);
    }
    
    /**********************************************************************************/
    //三种生成参数对象
    /**********************************************************************************/
    
    public static CallParam in(Object value)
    {
        return new CallParam(CALL_TYPE.IN, value);
    }
    
    public static CallParam inout(Object value)
    {
        return new CallParam(CALL_TYPE.INOUT, value);
    }
    
    public static CallParam out(Class<?> clazz)
    {
        return new CallParam(CALL_TYPE.OUT, clazz);
    }
}
